package com.ruoyi.pachong;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.pachong.domain.HpageNews;
import com.ruoyi.pachong.service.IHpageNewsService;

@Component("HubTask")
public class HubTask {
	
	public void ryParams(String params)
    {
        System.out.println("HubTask 执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("HubTask 执行无参方法");
        pNews();
        System.out.println("HubTask 执行无参方法结束");
    }
    
    @Autowired
    private IHpageNewsService  hpageNewsService;
    
    private void pNews() {
    	 
    	System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
         
         //新增一个预操作对象
           ChromeOptions options =new ChromeOptions();
           //删除提示栏
           //options.addArguments("disable-infobars");

           // 设置后台静默模式启动浏览器
           options.addArguments("--headless");
           options.addArguments("--no-sandbox");
//           options.addArguments("--disable-gpu");
//           options.addArguments("--disable-dev-shm-usage");        
           options.addArguments("--window-size=1920,1050");
           
           Map<String, Object> prefs = new HashMap<String, Object>();
           //禁止弹出通知
           prefs.put("profile.default_content_setting_values.notifications", 2);

           //禁止加载图片
           prefs.put("profile.managed_default_content_settings.images", 2);

           //禁止加载css
           prefs.put("permissions.default.stylesheet", 2);

           //options.setExperimentalOption("prefs", prefs);
           
           //options.addArguments("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36");
           
           WebDriver driver = new ChromeDriver(options);
           
           driver.manage().window().maximize();
           driver.get("https://tophub.today/");
           //获取所有类目div
           List<WebElement> allels =  driver.findElements(By.id("Sortable"));
           String uuid = UUID.fastUUID().toString(true);
           System.out.println("共有行数："+allels.size());
           //List<HpageNews> leimulist = new ArrayList<HpageNews>();
           //遍历每一行  div代表一行类目
           for(WebElement div : allels) {
           	//遍历每一个类目内部div，lanmudiv代表一个类目
           	
           	
           	List<WebElement> yihanggeshudivs = div.findElements(By.cssSelector("div.cc-cd"));
           	for(WebElement lanmudiv : yihanggeshudivs) {
           		System.out.println("一行内类目个数："+yihanggeshudivs.size());
           		//遍历一个类目的内容 HpageNewsdiv代表类目的各部分内容div   
           		List<WebElement> div3 = lanmudiv.findElement(By.tagName("div")).findElements(By.xpath("div"));
           		
           		System.out.println("类目内容："+div3.size());
           		
           		String leimu = div3.get(0).findElement(By.tagName("span")).getText();
           		//HpageNews nr = new HpageNews();
           		System.out.println("类目："+leimu);
           		//nr.setNewTypeName(leimu);
           		List<WebElement> els = div3.get(1).findElement(By.tagName("div")).findElements(By.xpath("a"));
           		
           		
           		//List<HpageNews> HpageNewslist = new ArrayList<HpageNews>();
           		for(WebElement wel : els) {
           			HpageNews hnews = new HpageNews();
           			hnews.setNewTypeName(leimu);
           			hnews.setNewUuid(uuid);
                   	String href = wel.getAttribute("href");
                   	//System.out.println("链接："+href);
                   	hnews.setNewUrl(href);
                   	List<WebElement> spans = wel.findElements(By.tagName("span"));
                   	if(StringUtils.isNotEmpty(spans.get(0).getText())) {
                   		hnews.setNewOrder(Long.parseLong(spans.get(0).getText()));
                   	}else {
                   		hnews.setNewOrder(1l);
                   	}
                   	
                   	//nr.setIndexNum(spans.get(0).getText());
                   	hnews.setNewTitle(spans.get(1).getText());
                   	hnews.setNewHot(spans.get(2).getText());
                   	//HpageNewslist.add(nrow);
                   	//nr.setHpageNewslist(HpageNewslist);
                   	int re = hpageNewsService.insertHpageNews(hnews);
                   }
           		
           		//leimulist.add(nr);
           		
           	}
           	
           	
           	//System.out.println(leimulist.size());	
           	
           	
           	
           }
           //System.out.println(leimulist.size());	
         //关闭浏览器
           driver.close();
           driver.quit();
    }
}
